#include <cstring>
#include "person.h"

using namespace std;

namespace A {
    Person::Person(){

    }

    Person::Person(char *name, int age) {
        this->name = name;
        this->age = age;
    }

    Person::Person(const Person &p) {
        cout << "Person(Person &p)" << endl;
        this->name = new char[strlen(p.name) + 1];
        strcpy(this->name, p.name);

        this->age = age;

        this->work = new char[strlen(p.work) + 1];
        strcpy(this->work, p.work);
    }

    void Person::setName(char *name) {
        this->name = name;
    }

    void Person::setAge(int age) {
        this->age = age;
    }

    void Person::setWork(char *work) {
        this->work = work;
    }

    void Person::printInfo(void) const{
        cout << "name = " << name << ", age = " << age << ", work = " << work << endl;
    }

    void printVersion(void) {
        printf("Person v1, by waylon\n");
    }

    Person& Person::operator=(const Person& p) {
        cout << "Person::operator=" << endl;
        if (this == &p)
            return *this;
        this->age = p.age;
        if(this->name) {
            cout << "name = " << name << endl;
            delete this->name;
        }

        if (this->work) {
            cout << "work = " << work << endl;
            delete this->work;
        }

        this->name = new char[strlen(p.name) + 1];
        strcpy(this->name, p.name);

        this->work = new char[strlen(p.work) + 1];
        strcpy(this->work, p.work);

        return *this;
    }
}